<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 会员类
 *
 * @author      Eden
 * @datetime    2016/12/3 13:16
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Index_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表
     * @param $params
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public function getLists($params, $offset = 0, $limit = 20)
    {
        try {

            $pageDb = clone($this->db);
            $adminid = adminId();
            if ($adminid == 2) {
                $pageDb->where('virtual', 0);
            }
            if (!empty($params['kw'])){
                $this->db->like('nickName', $params['kw'], 'both', "_");
                $pageDb->like('nickName', $params['kw'], 'both', '_');
            }

            if (!empty($params['status']) && in_array($params['status'], array('enable', 'disable'))) {
                $status = $params['status'] == 'enable' ? 1 : 0;
                $this->db->where('status', $status);
                $pageDb->where('status', $status);
            }

            $total = $pageDb->count_all_results('user');
            if ($total == 0) {
                return array(
                    'total' => 0,
                    'data' => array()
                );
            }

            if ($adminid == 2) {
                $this->db->where('virtual', 0);
            }
            $this->db->order_by('id', 'DESC');
            $this->db->limit($limit, $offset);
            $this->db->join('user_profile', 'user_profile.userId = user.id');
            $query = $this->db->get('user');
            $result = $query->result_array();

            return array(
                'total' => intval($total),
                'data' => $this->_afterSelect($result)
            );

        } catch(PDOException $e) {
            return false;
        }

    }

    /**
     * 处理查询结果
     * @param $data
     * @return array
     */
    private function _afterSelect($data)
    {
        $return = array();
        $this->load->library('core/Upload', null, 'up');
        if (is_array($data)) foreach ($data as $item) {
            $avatar = $this->up->bigResThumb('avatar', $item['avatar'], 'frontend');
            $return[] = array(
                'id' => intval($item['id']),
                'phone' => value($item, 'phone'),
                'nickName' => value($item, 'nickName'),
                'score' => intval($item['score']),
                'lastIp' => value($item, 'lastIp'),
                'lastTime' => value($item, 'lastTime'),
                'created' => value($item, 'created'),
                'status' => intval($item['status']),
                'source' => value($item, 'source'),
                'avatar' => $avatar,
                'amount' => intval($item['amount']),
                'amount_cny' => '￥' . money_format('%i', $item['amount'] / 100),
                'amountFreeze' => intval($item['amountFreeze']),
                'amountFreeze_cny' => '￥' . money_format('%i', $item['amountFreeze'] / 100),
                'amountBuy' => intval($item['amountBuy']),
                'amountBuy_cny' => '￥' . money_format('%i', $item['amountBuy'] / 100),
                'amountCashout' => intval($item['amountCashout']),
                'amountCashout_cny' => '￥' . money_format('%i', $item['amountCashout'] / 100),
                'amountUnLimit' => intval($item['amountUnLimit']),
                'amountUnLimit_cny' => '￥' . money_format('%i', $item['amountUnLimit'] / 100),
                'trueName' => value($item, 'trueName')
            );
        }

        return $return;
    }
}
